<template>
  <div>
    <MyStudentInfo :slist="studentlist" @addlist="onStudentAdd"></MyStudentInfo>
  </div>
</template>

<script>
import { ref, nextTick, provide } from 'vue'
import MyStudentInfo from './components/studentinfo.vue'
import request from './utils/request.js'

export default {
  name: 'MyApp',
  data() {
    return {
      //学生信息表
      studentlist: [],
      //新增学生信息表
      addstudent: []
    }
  },
  created() {
    this.getStudentList()
  },
  methods: {
    async getStudentList() {
      //发起请求
      const { data: res } = await request.get('/')
      //判断是否拿到数据
      if (res.length === 0) {
        return alert('请求学生列表数据失败!')
      }
      //将请求回来的数据保存到数组中
      this.studentlist = res
      // console.log(this.studentlist);
    },

    //监听传回来的新增学生数据
    async onStudentAdd(e) {
      this.addstudent = e
      const { data: res } = await request.post('/add', this.addstudent)
      // console.log(res);
    }
  },
  components: {
    MyStudentInfo
  },
  setup() {
    const sendGrandson = function (value) {

      console.log('孙子传值给我', value.aid)
      request.put('/update', {
        id: value.aid,
        name: value.aname,
        major: value.aclass,
        adminssionDate: value.adate
      })
      window.location.reload();
    }
    provide('sendHandle', sendGrandson)
  }
}
</script>
<style lang="less" scoped>

</style>
